import gevent

from src import app, CustomBlueprint
from src.baseapp.controller.caseService.caseController import CaseController
from src.baseapp.controller.caseService.directoryController import DirectoryController
from flask import Blueprint, Response

from src.baseapp.requestVerify.BaseRequestVerify import *
from src.baseapp.requestVerify.caseService.caseRequestVerify import *
from src.utils.execlUtil import case_file_to_dict
import json

# 设置这个模块的路由前缀
case_bp = CustomBlueprint('caseService', __name__, url_prefix='/caseService')


@case_bp.route('/getList', methods=['GET'], header_verify=HeadersSchema, body_verify=CaseListBodySchema)
def getCaseList(request_obj):
    """
    获取用例列表
    headers:
        token: 用户token
        context-type: 请求类型
        trace-id: 请求id
    body:
        directory_id: 目录id - int
        testcase_id: 用例id - int
        name: 用例名称 - str
        priority: 用例优先级 - array
        creator: 创建人 - int
        create_time: 创建时间 - array
        update_time: 更新时间 - array
        is_delete: 是否删除 - int
    """
    with CaseController() as case:
        result = case.get_case_list(request_obj.body.to_dict)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@case_bp.route('/case', methods=['GET'], header_verify=HeadersSchema, body_verify=CaseInfoBodySchema)
def getCaseInfo(request_obj):
    """
    获取用例详情
    headers:
        token: 用户token
        context-type: 请求类型
        trace-id: 请求id
    body:
        testcase_id: 用例id - int
    """
    testcase_id = request_obj.body.testcase_id
    with CaseController() as case:
        result = case.get_case_detail(testcase_id)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@case_bp.route('/case', methods=['POST'], header_verify=HeadersSchema, body_verify=AddCaseBodySchema)
def createCase(request_obj):
    """
    创建用例
    headers:
        token: 用户token
        context-type: 请求类型
        trace-id: 请求id
    body:
        cases_info: 用例信息 - array
    """
    with CaseController() as case:
        result = case.add_case(request_obj.body.cases_info)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@case_bp.route('/case', methods=['DELETE'], header_verify=HeadersSchema, body_verify=DeleteCaseBodySchema)
def deleteBatchCase(request_obj):
    """
    删除用例
    headers:
        token: 用户token
        context-type: 请求类型
        trace-id: 请求id
    body:
        case_ids: 用例id - array
    """
    case_ids = request_obj.body.case_ids
    with CaseController() as case:
        result = case.delete_case(case_ids)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@case_bp.route('/case', methods=['PUT'], header_verify=HeadersSchema, body_verify=UpdateCaseBodySchema)
def updateCase(request_obj):
    """
    更新用例
    headers:
        token: 用户token
        context-type: 请求类型
        trace-id: 请求id
    body:
        case_ids: 用例id - array
        modify_params: 修改参数 - object
    """
    update_params = request_obj.body.modify_params
    case_ids = request_obj.body.case_ids
    with CaseController() as case:
        result = case.update_case(case_ids, update_params)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@case_bp.route('/import', methods=['POST'], header_verify=ImportCaseHeadersSchema, body_verify=ImportCaseBodySchema)
def importCase(request_obj):
    """
    导入用例
    headers:
        token: 用户token
        context-type: 请求类型(文件)
        trace-id: 请求id
    body:
        directory_id: 目录id - int
    """
    def async_task():
        directory_id = request_obj.body.directory_id
        file = request.files['file_name']

        # execl内容处理
        cases_info = case_file_to_dict(file)
        for case in cases_info:
            case['directory_id'] = directory_id
        with CaseController() as case:
            case.add_case(cases_info)

    gevent.spawn(async_task)
    result = CaseController.response_data()
    resp = Response(json.dumps(result), status=200, mimetype='application/json')
    return resp


@case_bp.route('/directory', methods=['GET'], header_verify=HeadersSchema, body_verify=DirectoryListBodySchema)
def getDirectoryList(request_obj):
    """
    获取目录列表
    """
    with DirectoryController() as directory:
        if "directory_id" in request_obj.body.to_dict:
            result = directory.get_directory_by_id(request_obj.body.directory_id)
        elif "directory_name" in request_obj.body.to_dict:
            result = directory.get_directory_by_name(request_obj.body.directory_name)
        else:
            result = directory.get_all_directory()
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@case_bp.route('/directory', methods=['POST'], header_verify=HeadersSchema, body_verify=CreateDirectoryBodySchema)
def createDirectory(request_obj):
    """
    创建目录
    """
    name = request_obj.body.name
    parent_id = request_obj.body.get('parent_id', -1)

    with DirectoryController() as directory:
        result = directory.add_directory(name, parent_id)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@case_bp.route('/directory', methods=['DELETE'], header_verify=HeadersSchema, body_verify=DeleteDirectoryBodySchema)
def deleteDirectory(request_obj):
    """
    删除目录
    """
    with DirectoryController() as directory:
        result = directory.delete_directory(request_obj.body.directory_id)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@case_bp.route('/directory', methods=['PUT'], header_verify=HeadersSchema, body_verify=UpdateDirectoryBodySchema)
def updateDirectory(request_obj):
    """
    更新目录
    """
    directory_id = request_obj.body.directory_id
    name = request_obj.body.get('name', None)
    parent_id = request_obj.body.get('parent_id', None)

    with DirectoryController() as directory:
        if name:
            result = directory.update_directory(directory_id, name)
        if parent_id:
            result = directory.move_directory(directory_id, parent_id)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


app.register_blueprint(case_bp)
